
****************************************************************
* Authors: Henrik Andersson & Sirus Dehdari
* THIS IS THE MASTER FILE, WHICH DESCRIBES THE CONTENT AND THE ORDER 
* OF THE DO-FILES AND R-SCRIPTS USED TO GENERATE THE DATA AND PERFORM 
* THE ANALYSIS IN ANDERSSON AND DEHDARI (2021) - 
* "WORKPLACE CONTACT AND SUPPORT FOR ANTI-IMMIRATION PARTIES". 
*****************************************************************

////OVERALL NOTE ON THE DATA///
*The raw data used in the analyses are not open acess, 
*but for this project stored at server at Uppsala University. 
*With a few exeptions (e.g. election results per districts) the data 
*used represent individual level anonymized data from different
*administrative registers, which we are not allowed to share with 
*third parties. These data can instead be ordered from
*Statistics Sweden (see https://www.scb.se/en/services/guidance-for-researchers-and-universities/)


////////////////////////////////////////////
//GENERATE DATA TO ANALYSE in 5 do-files.
////////////////////////////////////////////

///1. PREPARE OUTCOME VARIABLE -  ELECTION RESULTS 
// AND TAKE OUT ELECTION DISTRICT ID:s for each ind.

do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\management_electionresults"

///2. PREPARE POPULATION WEIGHTS, used for better matching of precincts over time
//THIS TO TRANSLATE 2006/2010 PRECINCTS INTO 2014 PRECINCTS

do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\merge_precincts"


///3. USE MATCHED INDIVVDUAL/WORKPLACE DATA TO CREATE A VARIABLE FOR NUMBER 
* AND SHARE OF IM-COLLEAGUES (treatment variable)
* ALSO CREATES ALL OTHER RELEVANT MEASURES RELATED TO WORKPLACES

do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\Workplacedata"  


///4. Merge individual data on demographics and socioeconomics with individual level
* workplace data from (3.) and election results from (1.), collapse everything on
* precinct level  
*Also prepares information for labor market regions used as control

do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\management_inddata_collapse"


///5. Use population weights from (2.) and collapse the precinct 
* data created in (4.) to create comparable units over time. 
* Merges everyhing to a final panel data file with information on many
*precincts over three years (2006, 2010 and 2014).
do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\compute_shares"


///////////////
//ANALYSES //
////////////

//The following do-file creates all descriptves used in the online appendix
*including Figures B1 and B2, Tables B1, B3 and B4 
do "C:\Userdata\Shared\Dofiles\DoAnalysis\IntegrationSD\descriptives"


///All REGRESSIONS

*including Tables 1,2 and 4 from main paper 
*Tables C1-C9 in robstness check in online appendix
*Figure C1 in robstness check in online appendix

do "C:\Userdata\Shared\Dofiles\DoAnalysis\IntegrationSD\regressions"


///NOTE on the results///
*For Tables 2 and C1/C2; we first use the stata function 'cumul' to achieve
*a cumulative distribution of our sample along number of workers and
*population, and then divide our sample into over/under median, and deciles. 
*The default in cumul is not to assign the same percentile to tied values.
*For this reason, every time the code is rerun the assignment to above or below
*median will be slightly different. Someone replicating the results might 
*therefore not arrive at the exact same coefficients in Tables 2, C1 and C2.
*These potential differences should, however, be minimal 
*(for example changing the third decimal of the point estimate). 


///OTHER AUXILLIARY FIGURES AND ANALYSIS FROM ONLINE APPENDIX

*Survey used for correlation between preferences for refugee policy and moving patterns
*- creates Figure C3.
do "C:\Userdata\Shared\Dofiles\DoAnalysis\IntegrationSD\corr_based_on_salty"

*notices for job loss and routine task index - creates Figure D1
do "C:\Userdata\Shared\Dofiles\DoAnalysis\IntegrationSD\RTI_notices"


///MEASURMENT ERROR TEST (produces measurment error regression analysis in appendix B)
*Table B5
*Very time consuming file
do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\Measurement_errortest"





////////////////////////////
//ANALYSES THAT REQUIRES R /
////////////////////////////

///Descriptives in the main paper: 

*Figure 2 and Table B2 (in online appendix) created in R, using the R-scripts: 
*"share_noneu_figure.R" with help-file "se_comp_R"

*this may need installing some R-packages


///Online appendix: Figure for random addignment to workplaces 
*Figure C2
//Start by generating individual shares of foreign born for each worker 
do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\random_workers"

*This script in R creates a random assignment of 1000 random foreign born shares: 
*"randomize_shares.R"

*This do-file puts it together and creates Figure C...
do "C:\Userdata\Shared\Dofiles\DoData\IntegrationSD\Randomization attempt"
